 /**
 * @file tal_poll_manager.h
 * @brief Common process - poll manager
 * @version 1.0.0
 * @date 2021-09-09
 *
 * @copyright Copyright 2018-2021 Tuya Inc. All Rights Reserved.
 *
 */
#ifndef __TAL_POLL_MANAGER_H__
#define __TAL_POLL_MANAGER_H__


#ifdef __cplusplus
    extern "C" {
#endif

#include "tuya_cloud_types.h"
#include "tuya_zigbee_stack.h"


/*
 * zigbee poll result
*/
typedef enum {
    TAL_POLL_IDLE = 0,
    TAL_POLL_SUCESS,        ///< poll succeeded and get remote data
    TAL_POLL_BUSY_FAIL,     ///< poll failed because the data could not be sent out for some reasons(CCA fail or not low buff)
    TAL_POLL_NO_ACK_FAIL,   ///< poll failed because no MAC ack receive
} TAL_POLL_STATUS_E;

/*
 * zigbee poll configuration
*/
typedef struct {
    UINT8_T   forever_flag;             ///< TRUE: forever poll if device no network(TAL_ZG_NWK_POWER_ON_ONLINE/TAL_ZG_NWK_JOIN_OK/TAL_ZG_NWK_REJOIN_OK)
    UINT8_T   fast_swicth_parent;       ///< TRUE: if ack isn't received, will continue polling until the max number of failures is reached before switching the parent
    UINT16_T  interval_ms;              ///< poll period (bet: ms)
    UINT16_T  max_failed_times;         ///< enter parent-lost status when poll failed x times.
    UINT16_T  duration_after_data_ms;   ///< tiemout waitting app ack (bet: ms)
    UINT_T    duration_after_join_ms;   ///< wakeup time when join success.(for gateway get cluser data) (bet:ms)
    UINT_T    duration_after_rejoin_ms; ///< wakeup time when rejoin success.(for app use) (bet:ms)
} TAL_ZG_POLL_CFG_T;

/**
 * @brief send poll data(data request frame)
 *
 * @param[in]   none
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tal_zg_poll_send(VOID_T);

/**
 * @brief start continuous polling according to user settings
 *
 * @param[in]   none
 * @return none
 */
VOID_T tal_zg_poll_start(VOID_T);

/**
 * @brief stop continuous polling 
 *
 * @param[in]   none
 * @return none
 */
VOID_T tal_zg_poll_stop(VOID_T);

/**
 * @brief set polling interval 
 *
 * @param[in]   interval_ms: polling interval
 * @return none
 */
VOID_T tuya_zg_poll_interval_set(UINT16_T interval_ms);

/**
 * @brief set the maximum number of polling failures
 *
 * @param[in]   times: max number of polling failures
 * @return none
 */
VOID_T tal_zg_poll_max_failed_times_set(UINT16_T times);

/**
 * @brief polling complete callback
 * @note < this is a weak function you can rewrite this API in app layer >
 *
 * @param[in]   status: polling status
 * @return none
 */
VOID_T tal_zg_poll_complete_callback(TAL_POLL_STATUS_E status);



#ifdef __cplusplus
}
#endif

#endif

